﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Customer_Profile.Admin.DBDao;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.Admin
{
    public partial class UserModify : System.Web.UI.Page
    {
        protected static int UserId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();

                if (Request.QueryString["ID"] != null)
                    UserId = Convert.ToInt32(Request.QueryString["ID"].ToString());
                else
                {
                    KVB.Common.JavaScript.ViewMsgGoUrl("UserManage.aspx", " You Must Select！");
                }
                if (UserId != -1)
                {
                    DataSet ds = UserDao.getUserInfo(UserId);
                    txtLoginID.Value = ds.Tables[0].Rows[0]["User_LoginID"].ToString();
                    txtUserName.Value = ds.Tables[0].Rows[0]["User_Name"].ToString();
                    //txtUserPass.Value = ds.Tables[0].Rows[0]["User_Password"].ToString();                    
                    dropEntity.Items.FindByValue(ds.Tables[0].Rows[0]["User_EntityID"].ToString()).Selected = true;
                    txtRemark.Value = ds.Tables[0].Rows[0]["User_Remark"].ToString();
                    if (ds.Tables[0].Rows[0]["lvl_type"].ToString().Equals("G"))
                    {
                        rType.SelectedIndex = 1;
                        div1.Style.Add("display", "None");
                    }
                    else
                    {
                        rType.SelectedIndex = 0;
                        div1.Style.Add("display", "block");
                    }

                    if (ds.Tables[0].Rows[0]["USED_YN"].ToString() == "Y")
                        drpUsed.SelectedIndex = 0;
                    else
                        drpUsed.SelectedIndex = 1;
                }
            }
        }

        protected void ibtnModify_ServerClick(object sender, EventArgs e)
        {
            UserInfoEntity userCon = new UserInfoEntity();
            userCon.UserId = UserId.ToString();
            userCon.UserLoginId = txtLoginID.Value;
            userCon.UserName = txtUserName.Value;
            //userCon.UserPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtUserPass.Value.Trim(), "MD5").ToLower(); //txtUserPass.Value;
            userCon.UserEntityID = dropEntity.SelectedItem.Value;
            userCon.UserOffice = dropEntity.SelectedItem.Text;
            userCon.UserRemark = txtRemark.Value;
            userCon.Used = drpUsed.SelectedValue.ToString();
            userCon.UserlvlType = rType.SelectedValue.ToString().Trim();
            
            Hashtable htUserInfo = new Hashtable();
            htUserInfo.Add("UserInfo", userCon);

            try
            {
                bool isSucess = UserDao.updateUserInfo(htUserInfo);  //, locationList);
                if (isSucess == true)
                {
                    Response.Write("<script language=javascript>alert('Succeed!');</script>");

                    //记录用户操作日志
                    //CVOC.Comm.WriteSystemLog(Session["SS_USER_ID"].ToString(), Utility.ReplaceChar(Utility.ReplaceQuote(Request["menu_idx"].ToString()), true), "03", Page.Request.UserHostAddress, Page.Request.Path, "菜单修改操作，修改后的菜单名称为：" + txtName.Value);
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Error');</script>");
            }
        }

        protected void ibtnBack_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("UserManage.aspx", true);
        }
    }
}